<template>
  <!-- 添加或修改附件管理对话框 -->
  <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body width="80vw">
    <el-alert style="margin-bottom: 20px"
      title="该模块内容一般由业务模块上传，不建议自行录入"
      type="warning">
    </el-alert>

    <el-form ref="form" :model="form" :rules="rules" label-width="110px" style="padding: 0 24px">
      <form-block-title title="业务信息"/>
      <el-row :gutter="48">
        <el-col :sm="12" :xs="24">
          <el-form-item label="上传功能模块" prop="moduleName">
            <el-input v-model="form.moduleName" placeholder="请输入上传功能模块"/>
          </el-form-item>
        </el-col>
        <el-col :sm="12" :xs="24">
          <el-form-item label="上传控件标识" prop="uploaderId">
            <el-input v-model="form.uploaderId" placeholder="请输入上传控件标识"/>
          </el-form-item>
        </el-col>
        <el-col :sm="12" :xs="24">
          <el-form-item label="业务ID" prop="businessKey">
            <el-input v-model="form.businessKey" placeholder="请输入业务ID"/>
          </el-form-item>
        </el-col>
      </el-row>

      <form-block-title title="文件信息"/>
      <el-row :gutter="48">
        <el-col :sm="12" :xs="24">
          <el-form-item label="附件显示名称" prop="originalName">
            <el-input v-model="form.originalName" placeholder="请输入附件显示名称"/>
          </el-form-item>
        </el-col>
        <el-col :sm="12" :xs="24">
          <el-form-item label="附件保存文件名" prop="fileName">
            <el-input v-model="form.fileName" placeholder="请输入附件保存文件名"/>
          </el-form-item>
        </el-col>
        <el-col :sm="12" :xs="24">
          <el-form-item label="附件类型" prop="fileExt">
            <el-input v-model="form.fileExt" placeholder="请输入附件类型"/>
          </el-form-item>
        </el-col>
        <el-col :sm="12" :xs="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入内容" type="textarea"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">保 存</el-button>
      <el-button @click="cancel">关 闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {addAttachment, getAttachment, updateAttachment} from '@/api/attachment'
import FormBlockTitle from '@/components/ScFormBlockTitle/index'

export default {
  name: 'Edit',
  components: {FormBlockTitle},
  data() {
    return {
      id: -1,
      // 弹出层标题
      title: '录入信息',
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {}
      ,
      // 表单校验
      rules: {}
    }

  },
  created() {
  },
  methods: {
    init(id) {
      //编辑页面加载数据
      if (id && id !== -1) {
        this.id = id
        getAttachment(id).then(response => {
          this.form = response.data
        })
      }
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        moduleName: null,
        originalName: null,
        fileName: null,
        fileExt: null,
        fileSize: null,
        businessKey: null,
        uploaderId: null,
        md5: null,
        sha1: null
      }

      this.resetForm('form')
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAttachment(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.$emit('refreshDataList')
              }
            })
          } else {
            addAttachment(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.$emit('refreshDataList')
              }
            })
          }
        }
      })
    }
  }
}
</script>

